package com.daka.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import open189.sign.ParamsSign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TreeMap;

public class Token {
    protected static Logger log = LoggerFactory.getLogger(Token.class);
    private static final String TokenBasePath = "http://api.189.cn/v2/dm/randcode/token";

    public static String getToken(String access_token) throws Exception {
        TreeMap<String, String> paramsMap = new TreeMap<String, String>();
        paramsMap.put("app_id", SMS.app_id);
        paramsMap.put("access_token", access_token);
        paramsMap.put("timestamp", DateUtil.getToday());

        String getUrl = TokenBasePath + "?app_id=" + SMS.app_id
                + "&access_token=" + access_token + "&timestamp=" + DateUtil.getToday() + "&sign=" + ParamsSign.value(paramsMap, SMS.app_secret);
        String resJson = HttpInvoker.httpGet(getUrl);
        JSONObject jsonObject = JSON.parseObject(resJson);
        return jsonObject.getString("token");
    }


    public static void main(String[] args) {
        try {
            System.out.println(getToken("ed2a152b687a5c0ad2207bdbb8c261611399182005042"));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            log.error(e.getMessage());

        }
    }
}
